﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Thread3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始获取百度首页字符数量");
            Task<int> task1 = CountCharsAsync("http://www.google.com");
            Task<int> task2 = CountCharsAsync("http://www.baidu.com");

            CountDown();

            Console.WriteLine("谷歌:" + task1.Result);//等到Result才执行
            Console.WriteLine("百度:" + task2.Result);//等到Result才执行

            Console.ReadLine();
        }

        static async Task<int> CountCharsAsync(string url)
        {
            Console.WriteLine("开始下载" + url);
            WebClient wc = new WebClient();
            string result = await wc.DownloadStringTaskAsync(new Uri(url));
            wc.DownloadStringCompleted += wc_DownloadStringCompleted;
            Console.WriteLine("下载完成" + url);

            return result.Length;
        }

        static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            
        }

        private static void CountDown()
        {
            for (var i = 0; i < 10; i++)
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("countdown：{0}", i + 1);
            }
        }
    }
}
